#include "ModbusClient_dev.h"
#include "FloraLogger.h"
#include "ModbusClient.h"
//#define DEVICE_DEBUG
ModbusClient_dev::ModbusClient_dev(QObject *parent)
{
}

ModbusClient_dev::~ModbusClient_dev()
{

}
void ModbusClient_dev::setDeviceId(uint8_t device_id)
{
    m_device_id = device_id;
}
void ModbusClient_dev::read_registers(uint16_t address, uint16_t length, uint8_t cmdType, int askTime)
{
    ModbusRequest tempRequest ={0};
    tempRequest.opType = ReadRegisters;
    tempRequest.address = address;
    tempRequest.length = static_cast<uint8_t>(length) ;
    tempRequest.device_id = m_device_id;
    tempRequest.cmdType = cmdType;
    tempRequest.asktime = askTime;
    emit read_register(tempRequest);
}
void ModbusClient_dev::read_coils(uint16_t address, uint16_t length, uint8_t cmdType, int askTime)
{
    ModbusRequest tempRequest={0};
    tempRequest.opType = ReadCoils;
    tempRequest.address = address;
    tempRequest.length = static_cast<uint8_t>(length);
    tempRequest.device_id = m_device_id;
    tempRequest.cmdType = cmdType;
    tempRequest.asktime = askTime;
    emit read_register(tempRequest);
}
void ModbusClient_dev::write_registers(uint16_t address, uint16_t length, uint16_t *data, uint8_t cmdType, int askTime)
{
    ModbusRequest tempRequest={0};
    tempRequest.opType = WriteRegisters;
    tempRequest.address = address;
    tempRequest.length = static_cast<uint8_t>(length);
    tempRequest.device_id = m_device_id;
    memcpy(tempRequest.date, data, length * sizeof(uint16_t));
    tempRequest.cmdType = cmdType;
    tempRequest.asktime = askTime;
    emit read_register(tempRequest);
}

void ModbusClient_dev::write_coils(uint16_t address, uint16_t length, uint8_t *data, uint8_t cmdType, int askTime)
{
    ModbusRequest tempRequest={0};
    tempRequest.opType = WriteCoils;
    tempRequest.address = address;
    tempRequest.length = static_cast<uint8_t>(length);
    tempRequest.device_id = m_device_id;
    tempRequest.cmdType = cmdType;
    tempRequest.asktime = askTime;
    memcpy(tempRequest.date_8, data, length * sizeof(uint8_t));
    emit read_register(tempRequest);
}


void ModbusClient_dev::write_coil(uint16_t address, uint8_t data, uint8_t cmdType, int askTime)
{
    ModbusRequest tempRequest={0};
    tempRequest.opType = WriteCoil;
    tempRequest.address = address;
    tempRequest.device_id = m_device_id;
    tempRequest.date_8[0] = data;
    tempRequest.cmdType = cmdType;
    tempRequest.asktime = askTime;
    emit read_register(tempRequest);    

}

void ModbusClient_dev::write_int(uint16_t address, int data, int type, uint8_t cmdType, int askTime)
{
    ModbusRequest tempRequest={0};
    tempRequest.opType = WriteRegisters;
    tempRequest.address = address;
    tempRequest.device_id = m_device_id;
    tempRequest.length = 2;
    tempRequest.cmdType = cmdType;
    tempRequest.asktime = askTime;
    switch(type)
    {
    case IntType_AB:
        tempRequest.date[0] = data & 0xFF;
        tempRequest.date[1] = (data >> 8) & 0xFF;
        break;
    case IntType_BA:
        tempRequest.date[0] = (data >> 8) & 0xFF;
        tempRequest.date[1] = data & 0xFF;
        break;
    }
    emit read_register(tempRequest);
}

void ModbusClient_dev::setModbusClient(ModbusClient *cl){
    m_client = cl;
}

void ModbusClient_dev::reciveRequest(ModbusRequest request)
{
    LOG_DEBUG<<"reciveRequest result:"<<request.opType;
    switch(request.opType)
    {
    case ReadRegisters:
        break;
    case ReadCoils:
        break;
    case WriteRegisters:
        break;
    case WriteCoils:
        break;
    }
}

 uint8_t ModbusClient_dev::getDeviceId()
{
    return m_device_id;
}
